package com.example.demo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.demo.databinding.ActivityMainBinding;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity{

    private ActivityMainBinding binding;
    private static boolean isCheated = false;
    private static String KEY_INDEX = "index";
    //问题库
    private Question[] mQuestionLibrary = new Question[]{
            new Question(R.string.question_australia,true),
            new Question(R.string.question_oceans,true),
            new Question(R.string.question_air,true),
            new Question(R.string.question_radiation,false),
            new Question(R.string.question_temperature,false),
            new Question(R.string.question_phase,true),
            new Question(R.string.question_heat,false),
            new Question(R.string.question_stone,true),

    };
    //问题索引下标
    private int mCurrentIndex = 0;
    //把已回答的问题存入集合中
    private ArrayList<Integer> answeredIndex = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        //对当前题目索引恢复
        if (savedInstanceState!=null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX);
        }

//        binding.questionTv.setText(mQuestionLibrary[mCurrentIndex].getTextResId());

        //点击事件
        binding.trueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mQuestionLibrary[mCurrentIndex].setAnswered(true);
                checkAnswer(true);
                updateButtonClickable();
            }
        });
        binding.falseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mQuestionLibrary[mCurrentIndex].setAnswered(true);
                checkAnswer(false);
                updateButtonClickable();
            }
        });
        binding.nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1)%mQuestionLibrary.length;
                updateQuestion();
            }
        });
        binding.checkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,CheatActivity.class);
                intent.putExtra("trueQuestion", mQuestionLibrary[mCurrentIndex].isAnswerTrue());
                startActivityForResult(intent,0);
            }
        });


    }

    private void checkAnswer(boolean b) {
        boolean answerIsTrue = mQuestionLibrary[mCurrentIndex].isAnswerTrue();
        if (b == answerIsTrue){
            Toast.makeText(this,R.string.correct,Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,R.string.Incorrect,Toast.LENGTH_SHORT).show();
        }

    }

    private void updateButtonClickable() {
        if (mQuestionLibrary[mCurrentIndex].isAnswered()){
            binding.trueButton.setEnabled(false);
            binding.falseButton.setEnabled(false);
        }else {
            binding.trueButton.setEnabled(true);
            binding.falseButton.setEnabled(true);
        }
    }

    private void updateQuestion() {
        int question = mQuestionLibrary[mCurrentIndex].getTextResId();
        binding.questionTv.setText(question);
        updateButtonClickable();
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putInt(KEY_INDEX,mCurrentIndex);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        if (isCheated){
            Toast.makeText(this,R.string.judgment_toast,Toast.LENGTH_SHORT).show();
        }
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //获取作弊结果
        if (requestCode == 0 && resultCode == RESULT_OK){
            if (data == null){
                return;
            }
            isCheated = data.getBooleanExtra("answerShown",false);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}